package yl.hs.bmipfull.controllers.laboratory;


import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import yl.hs.bmipfull.controllers.ControllerBase;
import yl.hs.bmipfull.pojo.laboratory.ProductConfigDetailsEntity;
import yl.hs.bmipfull.service.laboratory.IProductConfigService;
import yl.hs.bmipfull.service.system.ISearchViewService;
import yl.hs.bmipfull.utils.Tl;
import yl.hs.bmipfull.viewmodels.PageQuery;
import yl.hs.bmipfull.viewmodels.SubmitModel;
import yl.hs.bmipfull.viewmodels.WebResult;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 作用:  产品配比表控制层<br/>
 * 创建时间: 2021年05月25日 11时24分32秒 <br/>
 *
 * @author 谢东
 */
@Api(value = "产品配比表Web接口", tags = "实验室管理——产品配比表操作")
@RequestMapping(value = "/api/product")
@RestController
public class ProductConfigController extends ControllerBase {

    @Autowired
    IProductConfigService service;
    @Autowired
    ISearchViewService svSVC;

    @ApiOperation(value = "查询产品配比表", notes = "查询产品配比表")
    @RequestMapping(value = "/config", method = RequestMethod.POST)
    public WebResult queryList(@ApiParam(value = "查询条件") @RequestBody PageQuery pageQuery) {
        try {
            return success("查询成功", service.queryList(pageQuery));
        } catch (Exception e) {
            return error(e);
        }
    }

    @ApiOperation(value = "根据产品名称、年份、季度查询产品配比表", notes = "根据产品名称、年份、季度查询产品配比表")
    @RequestMapping(value = "/config/{targetSN}/{useYear}/{useQuarter}", method = RequestMethod.GET)
    public WebResult queryListByTargetSN(@ApiParam(value = "产品编号") @PathVariable String targetSN,
                                         @ApiParam(value = "页码") @PathVariable int useYear,
                                         @ApiParam(value = "每页条数") @PathVariable int useQuarter) {
        try {
            return success("查询成功", service.queryListByTargetSN(targetSN, useYear, useQuarter));
        } catch (Exception e) {
            return error(e);
        }
    }

    @ApiOperation(value = "查询产品配比表", notes = "分页查询产品配比表")
    @RequestMapping(value = "/config/{rowNum}/{page}", method = RequestMethod.POST)
    public WebResult queryPage(@RequestBody PageQuery query,
                               @ApiParam(value = "页码") @PathVariable int page,
                               @ApiParam(value = "每页条数") @PathVariable int rowNum) {
        try {
            var result = service.queryPage(query, rowNum, page);
            svSVC.updateUserMenuView(query);
            return success("ok", result);
        } catch (Exception ee) {
            return error(ee);
        }
    }


    @ApiOperation(value = "查询产品配比表", notes = "根据产品配比表编号查询产品配比表")
    @RequestMapping(value = "/config/{sn}", method = RequestMethod.GET)
    public WebResult findBySn(@ApiParam(value = "编号") @PathVariable String sn) {
        return success("查询成功", service.findBySn(sn));
    }

    @ApiOperation(value = "根据年份和季度修改产品配比表", notes = "根据年份和季度修改产品配比表")
    @RequestMapping(value = "/config/{useYear}/{useQuarter}/{configType}/{targetSN}/{state}", method = RequestMethod.PUT)
    public WebResult changeStateByYearAndQuarter(@ApiParam(value = "年份") @PathVariable int useYear,
                                                 @ApiParam(value = "季度") @PathVariable int useQuarter,
                                                 @ApiParam(value = "季度") @PathVariable int configType,
                                                 @ApiParam(value = "季度") @PathVariable String targetSN,
                                                 @ApiParam(value = "状态") @PathVariable int state) {
        Integer result;
        try {
            result = service.changeStateByYearAndQuarter(useYear, useQuarter, configType, targetSN, (short) state);
            if (result > 0) {
                return success("保存成功");
            } else {
                return fail("未找到数据或数据已启用");
            }
        } catch (Exception e) {
            return error(e);
        }

    }

    @ApiOperation(value = "保存产品配比表", notes = "查询/更新产品配比表信息")
    @RequestMapping(value = "/config", method = RequestMethod.PUT)
    public WebResult saveMenu(@ApiParam(value = "提交信息") @RequestBody SubmitModel submitModel) {
        Integer result;
        try {
            List<Map<String, Object>> datas = (List<Map<String, Object>>) submitModel.getData().get("formItem");
            List<ProductConfigDetailsEntity> itemList = new ArrayList<>();
            for (var item : datas) {
                ProductConfigDetailsEntity entity = new ProductConfigDetailsEntity();
                Tl.getChangeLog(entity, item);
                itemList.add(entity);
            }
            result = service.saveProductConfig(submitModel.getData(), submitModel.getSn(), itemList);
            if (result > 0) {
                return success("保存成功");
            } else {
                return fail("数据未保存");
            }
        } catch (Exception e) {
            return error(e);
        }

    }

    @ApiOperation(value = "删除产品配比表信息", notes = "根据产品配比表编号删除系产品配比表信息")
    @RequestMapping(path = "/config", method = RequestMethod.DELETE)
    public WebResult delete(@ApiParam(value = "产品配比表编号", name = "sn", allowMultiple = true, required = true) @RequestParam("sn") String[] sn) {
        if (service.delete(sn) > 0) {
            return success("删除成功");
        } else {
            return fail("删除失败");
        }

    }

    @ApiOperation(value = "根据年份，季度修改配比状态", notes = "根据年份，季度修改配比状态")
    @RequestMapping(path = "/changebyyear/{useYear}/{useQuarter}/{state}", method = RequestMethod.GET)
    public WebResult changesByYear(@ApiParam(value = "年份") @PathVariable int useYear, @ApiParam(value = "季度") @PathVariable int useQuarter, @ApiParam(value = "状态") @PathVariable short state) {
        try {
            var result = service.changesByYear(useYear, useQuarter, state);
            if (result > 0) {
                return success("成功启用" + result + "条配比");
            } else {
                return success("未可启用配比");
            }
        } catch (Exception ee) {
            return error(ee);
        }
    }
    @ApiOperation(value = "根据产品配比编号修改状态", notes = "根据产品配比编号修改状态")
    @RequestMapping(value = "/config/changeStateBySn/{sn}/{state}", method = RequestMethod.GET)
    @ResponseBody
    public WebResult changeStateBySn(@PathVariable String sn,@PathVariable short state) {
        try {
            Integer result = service.changeStateBySn(sn.split(","), state);
            if (result > 0) {
                return success("数据已修改");
            }
            return fail("没有修改的任何数据");
        } catch (Exception ee) {
            return error(ee);
        }
    }
}